/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.component.mail.service.processor;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import info.peigen.hotpot.common.service.base.processor.Processor;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceContext;
import info.peigen.hotpot.component.mail.domain.Mail;
import info.peigen.hotpot.component.mail.domain.MailAttachment;
import info.peigen.hotpot.component.mail.domain.MailTemplate;
import info.peigen.hotpot.component.mail.service.order.AbstractMailServiceOrder;
import info.peigen.hotpot.component.mail.service.order.MimeMailOrder;
import info.peigen.hotpot.component.mail.service.order.SimpleMailOrder;
import org.noear.solon.annotation.Component;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <b>(GenerateMailDomainProcessor)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2023/1/20
 */
@SuppressWarnings("rawtypes")
@Component
public class GenerateMailDomainProcessor extends AbstractMailServiceProcessor implements Processor {
    @Override
    public Integer level() {
        return 30;
    }

    @Override
    public void execute(ProcessorServiceContext serviceContext) {
        if (serviceContext.getOrder() instanceof MimeMailOrder order) {
            Mail mail = genMimeDomain(order);
            mail.setContext(order.getContext());
            parseTemplate(mail, order);
            parseAttachment(mail, order);
            serviceContext.setDomain(mail);
        }

        if (serviceContext.getOrder() instanceof SimpleMailOrder order) {
            Mail mail = genSimpleDomain(order);
            mail.setContext(order.getContext());
            serviceContext.setDomain(mail);
        }
    }

    private Mail genSimpleDomain(SimpleMailOrder order) {
        Mail mail = Mail.create(order);
        patch(mail, order);
        return mail;
    }

    private Mail genMimeDomain(MimeMailOrder order) {
        Mail mail = Mail.create(order);
        patch(mail, order);
        return mail;
    }

    private void patch(Mail mail, AbstractMailServiceOrder order) {
        mail.setSubject(Optional.ofNullable(order.getSubject()).orElse(email.getSubject()));
        mail.setFrom(Optional.ofNullable(order.getFrom())
                .orElse(Objects.requireNonNull(email.getFromRecipient()).getAddress()));
    }

    private void parseTemplate(Mail mail, MimeMailOrder order) {
        if (StrUtil.isNotEmpty(order.getTemplateId())) {
            MailTemplate template = mailTemplateRepository.active(order.getTemplateId());
            template.setTemplateMap(order.getTemplateMap());
            mail.setMailTemplate(template);
            mail.setContext(template.getContext());
        }
    }

    private void parseAttachment(Mail mail, MimeMailOrder order) {
        if (ObjUtil.isNotEmpty(order.getAttachments())) {
            List<MailAttachment> mailAttachments = order.getAttachments().entrySet().stream().map(file ->
                    MailAttachment.builder()
                            .attachmentName(file.getKey())
                            .attachmentUrl(file.getValue())
                            .build()
            ).collect(Collectors.toList());

            mail.setMailAttachments(mailAttachments);
        }
    }

}